package com.atstudy.dao;

import com.atstudy.entites.User;
import com.atstudy.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @version v1.0
 * @description:
 * @author: admin
 * @date: 2023/2/2 9:44
 * @copyright: 博为峰
 */
public class UserDaoImpl implements UserDao {
    @Override
    public User selectByName(String name) {
        Connection conn = null;
        User user = null;
        try {
            conn = DBUtil.getConn();
            PreparedStatement preparedStatement = conn.prepareStatement("select * from user where name = ?");
            preparedStatement.setString(1, name);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                user = new User(resultSet.getInt("id"), resultSet.getString("name"), resultSet.getString("password"));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            try {
                DBUtil.closeConn(conn);
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return user;
    }
}
